define(["jquery","bootstrap","bbcare","fu/app/comm/comm",
    "eva_Pub/vm/fiveHighRiskInfants/fiveHighRiskInfantsQuestionnaire.servers",
    "eva_Pub/vm/fiveHighRiskInfants/fiveHighRiskInfantsQuestionnaire.ui",
    "eva_Pub/vm/fiveHighRiskInfants/fiveHighRiskInfantsQuestionnaire.event"
],function ($,boostrap,$bb,$comm,$servers,$ui,$event) {
    var fiveHighRisk = {};
    fiveHighRisk.init = function(){
        var getParams = JSON.parse($bb.getSessionStorage('FHRIS'));
        sessionStorage.removeItem('FHRIS');
        /*
        * 题目答案与题目处理
        * */
        var dealScore = function(ques,score){
            for(var  i = 0;i< ques.length;i++){
                for(var  j = 0;j< ques[i].question.length;j++){
                    var id = ques[i].question[j].id;
                    var options =  ques[i].question[j].option;
                    if(ques[i].question[j].topic){
                        ques[i].question[j].value = score[id][0].answer;
                    }else{
                        for(var m = 0;m < options.length ; m++){
                            if(options[m].type == "textbox"){
                                var name = options[m].name[0];
                                for(var k = 0;k < score[id].length;k++){
                                    if(score[id][k].name == name){
                                        options[m].value = score[id][k].answer;
                                        options[m].colours = score[id][k].colour;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };
        /*
         * 获取题目：
         * */
        var getAllPostionCallback = function(data,params){
            if(params.score && params.score.length > 0){
                var score = {};
                for(var i = 0;i<params.score.length;i++){
                    score[params.score[i].id] = params.score[i].answer;
                }
                dealScore(data.data.quesiton,score);
            }
            $ui.questionnaireInit({"id":"#questionnaire","data":data.data.quesiton});
            $event.init(params);
        };
        /*
         * 获取用户的信息：
         * */
        var getPatientInfoCallback = function(data,getParams){
            var diff = $comm.dateDiff(data.PatientInfo.birthday,$comm.getCurTime().date);
            getParams.monthAge = $servers.correctMonthAge(diff,data.PatientInfo.pregnancyDay);
            $servers.getAllPostion(getParams , getAllPostionCallback,getParams);
        };
        /*
        * 获取历史问卷记录：
        * */
        var historyCallback = function(data,getParams){
            var score = [];
            if(data.rows.length > 0){
                score = JSON.parse(data.rows[0].answerResult);
            }
            var params = $.extend({},getParams);
            params.score = score;
            $servers.getPatientInfo(getParams,getPatientInfoCallback,params);
        };
        $servers.history(getParams,historyCallback,getParams);
    };
    return fiveHighRisk;
});